﻿// Example header text. Can be configured in the options.
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace vtech_frameWorks.Library
{
    public class ClsImages
    {
        #region "ImageToByteArray"
        /// <summary>
        /// Mã hóa ảnh ra mảng
        /// </summary>
        /// <param name="image">Images</param>
        /// <returns>Array</returns>
        public static byte[] ImageToByteArray(Image image)
        {
            if (image != null)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    image.Save(stream,ImageFormat.Png);
                    return stream.ToArray();
                }
            }
            else
            {
                return null;
            }
        }

        #endregion

        #region "ByteArrayToImage"
        /// <summary>
        /// Giải mã từ mảng ra ảnh
        /// </summary>
        /// <param name="bytes">Array</param>
        /// <returns>Images</returns>
        public static Image ByteArrayToImage(byte[] bytes)
        {
            if (bytes != null)
            {
                using (MemoryStream stream = new MemoryStream(bytes))
                {
                    return Bitmap.FromStream(stream);
                }
            }
            else
            {
                return null;
            }
        }
        #endregion
    }
}